<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	private $title = "电影" ;

	function _initialize(){
		parent::_initialize();
		$this->assign('meta_title' , "售票网");
	}

	//系统首页
    public function index(){
       

        $cid = $_SESSION['select_movie_id'] ;
      
        if(!empty($cid))
        {
             $this->assign('title' , "该影院正在放映");
        } else {
             $this->assign('title' ,   $this->title);
        }

    	$this->assign('current' ,0);
        $this->display(); 
    }

    //电影院 
    function cinema(){
    	$this->title = "影院" ;
    	$this->assign('current',1);
        $cinema = M('Cinema')->select();
        $this->assign('list',$cinema);
    	$this->assign('title' , $this->title);
    	$this->display();
    }

   

    //购买 
    function buy ($id = null )
    {
        if(!$id)
        {
            redirect(url('index/index'));
        }

        $cid = $_SESSION['select_movie_id'];

        if(!empty($cid))
        {
            redirect(url('index/detail',array('mid'=>$id,'cid'=>$cid)));
        }


       //通过电影id查询 所有的电影院并且在将来要放映此场电影的电影院
       $_SESSION['buy'] = $id ;
       $map = array();
       $map['m.mid'] = $id ; 
       $map['m.start_time'][0] = array('gt',strtotime(date('Y-m-d')));
       $map['m.start_time'][1] = array('lt',(strtotime(date('Y-m-d')) + 24 * 3600));
       $field = 'c.id,c.title,c.address,c.img,m.price';
       $list = M('Manage')->alias('m')->distinct(true)->field($field)
                  ->join("LEFT JOIN api_cinema AS c ON c.id = m.cid")
                  ->join("LEFT JOIN api_movies AS mv ON mv.id = m.mid")
                  ->where($map)->select();
       $res = array_pass($list , "title");
       $this->assign('list',$res);
       $this->assign('title','选择影院');
        $this->assign('current' , 1);
       $this->display();
    } 
    function selectmovies($cid = 0){
       if($cid != 0)
       {
         $_SESSION['select_movie_id'] = $cid ; 
       }
      
     redirect(url('index/index'));
    }
    /**
     * 详情页面
     */
    function detail($mid=nulll,$cid=null){

        if(!$mid || !$cid )
        {
            exit();
        }

        if(isset($_SESSION['select_movie_id']))
        {
            unset($_SESSION['select_movie_id']);
        }

        //电影院信息
        $cinema = M('Cinema')->where('id='.$cid)->find();
        //电影信息
        $movie = M('Movies')->where('id='.$mid)->find();

        $_SESSION['movies_info'] = array('movie'=>$movie , 'cinema'=>$cinema);

        //电影院场次信息
        $map = array('mid'=>$mid,'cid'=>$cid);
        $map['start_time'][0] = array('gt',strtotime(date('Y-m-d')));
        $map['start_time'][1] = array('lt',(strtotime(date('Y-m-d')) + 24 * 3600));

        $field = "r.name,r.id AS rid ,m.*";
        $manage = M('Manage')->alias('m')->field($field)
                        ->join("LEFT JOIN api_cinema_rooms AS r ON r.id = m.rid")
                        ->where($map)->select();
        //判断是否过期
        foreach($manage as $k=>$v)
        {
            if($v['start_time'] < time())
            {
                $manage[$k]['passed'] = 1;
            }
        }
 
       
        $this->assign('manage',$manage);
        $this->assign('movie',$movie);
        $this->assign('title',$cinema['title']);
        $this->assign('info',$cinema);
        $this->display();
    }

    /**
     * 在线选座位
     */
    function selectsit(){
        $movies_info = $_SESSION['movies_info'];
        if(!$movies_info)
        {
            exit();
        }

        $manageid = I('get.manageid');
        $map = array();
        $map['id'] = $manageid;
        $info = M('Manage')->where($map)->find();
        $this->assign('seat',$seat);
        $this->assign('title',$_SESSION['movies_info']['cinema']['title']);
        $this->assign('movie',$_SESSION['movies_info']['movie']);//选择此房间的座位
        $this->assign('info',$info);
        $this->assign('manageid',$manageid);
        $this->display();


    }

    /**
     * 支付页面
     */
    function pay(){
        $order_sn = I('get.orderNo');

        if(!is_login() || empty($order_sn)){

            die;
        }
        $user = session('user_auth') ;
        $map ['order_sn'] = $order_sn;
        $map ['order_status'] = 0 ;
        $map ['pay_status'] = 0 ;
        $map ['user_id'] = $user['uid'];

        //首先查询订单状态 
        $order = M('Orders')->where($map)->find();
   
        if(empty($order))
        {  
            //to do  已经支付或者无效订单
            exit;
        }
        $field = "om.order_sn,om.start_time,om.end_time,om.room_id,om.row,om.col," .
                 "c.title AS cname,c.address,".
                 "m.title AS mname,".
                 "r.name AS rname";
        //查询从表 
        $order_extend = M('OrderMovies')->alias('om')->field($field)
                        ->join("LEFT JOIN api_cinema AS c ON c.id=om.cinema_id")
                        ->join("LEFT JOIN api_movies AS m ON om.movie_id=m.id")
                        ->join("LEFT JOIN api_cinema_rooms AS r ON r.id=om.room_id")
                        ->where('om.order_sn='.$order_sn)->select();
        $this->assign('info',$order_extend);
        $this->assign('order_info',$order);
        $this->assign('title','订单支付');
        $this->display();
        
    }

    //个人中心
    function ucenter(){

        if(!is_login())
        {
            redirect(url('index/login'));
        }

        $this->title = "个人中心" ;
        $this->assign('current',3);
        $this->assign('title' , $this->title);
        $this->display();
    }
    //登录
    function login(){
            if(is_login())
            {
                redirect(url('index/ucenter'));
            }
            $from = I('get.from');
            $this->assign('from',$from);
            $this->assign("title","用户登录");
            $this->assign('current' , 3);
            $this->display();
    }
    //注册
    function register(){
            if(is_login())
            {
                redirect(url('index/ucenter'));
            }
            $this->assign("title","用户注册");
            $this->assign('current' , 3);
            $this->display();
    }

    //个人资料设置 
    function userinfo(){
        $this->assign("title","资料设置");
        $this->assign('current' , 3);
        $this->display();
    }

    //修改密码 
    function account(){
        $this->assign("title","修改密码");
        $this->assign('current' , 3);
        $this->display();
    }

   //订单 
    function orderlist($p = 1){

        $user = session('user_auth');
        $map = array();
        $map['user_id'] = $user['uid'];

        $list = M('Orders')->where($map)->limit(($p-1)*10,10)->order('id desc')->select();
        
        $this->title = "订单列表" ;
        $this->assign("list",$list);
        $this->assign('current',2);
        $this->assign('title' , $this->title);
        $this->display();
    }

    function map(){
        $this->assign('title' , '地理位置选择');
        $this->assign('current' , '0');
        $this->display();
    }


}